<?php
//if (isset($_SESSION['username'])) {
include_once 'header.php';
require_once("conn.php");
require_once("change_page.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>乐知家园</title>
        <script language="javascript">
            function deletepack(id,name,page){
                var str = "\u786e定删除 " + name + "\uff1f";
                if(confirm(str)){
                    location.href = "deletePack.php?ids=" + id + "&page=" + page;
                }
            }
            function search_key(){
                var key = document.getElementById("keyword").value;
                if(key==null||key==''){
                    alert("\u8bf7输入关键字");
                } else {
                    var page = document.getElementById("current_page").value;
                    location.href = "packList.php?keyword=" + key + "&page=" + page+"&menu=pack";
                }
            }
            function change_age(){
                var select_age = document.getElementById("select_age").value;
                var page = document.getElementById("current_page").value;
                location.href = "packList.php?select_age=" + select_age + "&page=" + page+"&menu=pack";
            }
            function click_all(e){
                var checkboxes = document.getElementsByName("pack_checkbox");
                for(var i=0;i<checkboxes.length;i++){
                    if(e.checked){
                        checkboxes[i].checked = true;
                    }else{
                        checkboxes[i].checked = false;
                    }
                }
            }
            function delete_pack_multi(){
                var str = "\u786e定删除选中的套？";
                if(confirm(str)){
                    var ids = "";
                    var checkboxes = document.getElementsByName("pack_checkbox");
                    for(var i=0;i<checkboxes.length;i++){
                        if(checkboxes[i].checked){
                            ids = ids + checkboxes[i].value + ",";
                        }
                    }			
                    location.href = "deletePack.php?ids=" + ids;
                }
            }
        </script>
        <style type="text/css">
            <!--
            body,td,th {
                font-family: 微软雅黑;
                font-size: 14px;
                color: #000000;
            }
            .STYLE1 {color: #FFFFFF}
            -->
        </style></head>
    <?php
    if (isset($_GET['select_age'])) {
        $select_age = $_GET['select_age'];
    } else {
        $select_age = -1;
    }
    ?>
    <body>
        <br/>
        <table width="80%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
            <tr align="left">
                <td bgcolor="#FFFFFF"><a href="packEdit.php?menu=pack">新建</a>&nbsp;<a href="#" onclick="delete_pack_multi()">删除</a></td>
                <td>搜索编号、名称 <input type="text" name="keywork" id="keyword" value="" /> <input type="button" value="搜索" onclick="search_key()" /></td>
                <td>按年龄筛选 <select name="select_age" id="select_age" onchange="change_age()">
                        <option value="-1">无</option>
                        <?php
                        for ($i = 0; $i < 6; $i++) {
                            ?>
                            <option value="<?php echo $i; ?>" <?php if ($select_age == $i) { ?> selected="selected" <?php } ?>><?php echo $i . "-" . ($i + 1) . "岁"; ?></option>
                        <?php } ?>
                    </select></td>				
                <td bgcolor="#FFFFFF" align="right"><?php
                        $page_size = 100;
                        if (isset($_GET['page'])) {
                            $page = $_GET['page'];
                        } else {
                            $page = 0;
                        }

                        $age_sql_where = "";
                        $age_sql_and = "";
                        if ($select_age != -1) {
                            $age_sql_where = " WHERE age = " . $select_age;
                            $age_sql_and = " AND age = " . $select_age;
                        }

                        if (isset($_GET['keyword'])) {
                            $keyword = $_GET['keyword'];
                            $keyword = trim($keyword);
                            $sql = "SELECT COUNT(*) AS total FROM t_pack WHERE (packcode LIKE '%" . $keyword . "%' OR name LIKE '%" . $keyword . "%') " . $age_sql_and;
                        } else {
                            $sql = "SELECT COUNT(*) AS total FROM t_pack " . $age_sql_where;
                        }

                        $db = new DBC();
                        $total_num = $db->fetchData($sql);
                        if ($total_num != null) {
                            $total_num_product = $total_num[0]['total'];
                        }
                        $page_count = getPageSize($total_num_product, $page_size);
                        echo getPage("packList.php?menu=pack", $page_count, $page);
                        ?></td>
            </tr>
        </table>
        <br/>
        <table width="80%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#9999FF">
            <tr align="center">
                <td bgcolor="#666666" width="5%"><input type="checkbox" name="all" onclick="click_all(this)" /><input name="current_page" id="current_page" type="hidden" value="<?php echo $page; ?>" /></td>
                <td bgcolor="#666666" width="7%"><span class="STYLE1">序号</span></td>
                <td bgcolor="#666666" class="STYLE1" width="8%">编号</td>
                <td bgcolor="#666666" class="STYLE1" width="25%">名称</td>
                <td bgcolor="#666666" class="STYLE1" width="10%">月份</td>
                <td bgcolor="#666666" class="STYLE1" width="10%">年龄</td>				
                <td bgcolor="#666666" class="STYLE1" width="20%">时间</td>                
                <td bgcolor="#666666" class="STYLE1" width="15%">操作</td>
            </tr>
            <?php
            $begin = $page * $page_size;
            $num = $begin;
            if (isset($_GET['keyword'])) {
                $keyword = $_GET['keyword'];
                $keyword = trim($keyword);
                $sql = "SELECT * FROM t_pack WHERE (packcode LIKE '%" . $keyword . "%' OR name LIKE '%" . $keyword . "%') " . $age_sql_and . " ORDER BY packcode LIMIT " . $begin . "," . $page_size;
            } else {
                $sql = "SELECT * FROM t_pack " . $age_sql_where . " ORDER BY packcode LIMIT " . $begin . "," . $page_size;
            }

            $data = $db->fetchData($sql);
            foreach ($data as $pack) {
                $age = "";
                for ($i = 0; $i < 6; $i++) {
                    if ($pack['age'] != '' && $pack['age'] == $i) {
                        $age = $i . "-" . ($i + 1) . "岁";
                    }
                }
                $year_month = $pack['year'] . "-" . $pack['month'];
                if ($pack['year'] == -1 && $pack['month'] == -1) {
                    $year_month = "无";
                }
                ?>
                <tr align="center">
                    <td bgcolor="#FFFFFF"><input type="checkbox" name="pack_checkbox" value="<?php echo $pack['packid']; ?>"/></td>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo++$num; ?></td>
                    <td bgcolor="#FFFFFF" align="left">&nbsp;<?php echo $pack['packcode']; ?></td>
                    <td bgcolor="#FFFFFF" align="left">&nbsp;<?php echo $pack['name']; ?></td>
                    <td bgcolor="#FFFFFF"> <?php echo $year_month; ?></td>                    
                    <td bgcolor="#FFFFFF"> <?php echo $age; ?></td>
                    <td align="center" bgcolor="#FFFFFF"><?php echo $pack['createTime']; ?></td>
                    <td align="center" bgcolor="#FFFFFF"><a href="packEdit.php?menu=pack&packid=<?php echo $pack['packid']; ?>">编辑</a>&nbsp;<a href="#" onclick="deletepack('<?php echo $pack['packid']; ?>','<?php echo $pack['packcode'] . "-" . $pack['name']; ?>','<?php echo $page; ?>')">删除</a></td>
                </tr>
            <?php } ?>                    
        </table>
    </body>
</html>
<?php
include_once 'footer.php';
//} else {
//    header("login.php");
//}
?>